﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;
using NTTHAO.Imaging;

public partial class admin_webparts_Plug_ins_Files_ResizeImage : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string _Message = string.Empty;
    public string _CurrentPath = "/";
    public string _CurrentItem = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave1.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        try
        {
            string action = Request.QueryString["action"];

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }

            string[] paths = _CurrentPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            _CurrentItem = paths[paths.Length - 1];
        }
        catch (Exception)
        {
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.Replace("/", "\\").Substring(1));
            
            int width = 0, height = 0;
            Int32.TryParse(txtWidth.Text.Trim(), out width);
            Int32.TryParse(txtHeight.Text.Trim(), out height);

            ImageUtilities.ResizeImageToJpeg(currentServerPath, width, height, ResizeOptions.FitWidth, System.Drawing.Color.White, ImageUtilities.STANDARD_PROGRESSIVE);

            if (chkLayerMask.Checked)
            {
                this.AddLayerMask(currentServerPath);
            }

            this.SetCookie(FILES_MESSAGE_TYPE, "0");
            this.SetCookie(FILES_MESSAGE, "Hình ảnh đã được thay đổi kích thước.");

            Response.Redirect("?type=page&name=files&file=" + Server.UrlEncode(this.ParentPath), false);
        }
        catch (Exception ex)
        {
            this._Message = ex.Message;
        }
    }

    protected void AddLayerMask(string serverFileName)
    {
        //layer mask
        if (Constant.ADD_LAYER_MASK)
        {
            float left = 0, top = 0, width = 0, height = 0, opacity = 1;
            float.TryParse(ConfigValues.GetConfigValue("MASK_TOP_PERCENT").Trim(), out top);
            float.TryParse(ConfigValues.GetConfigValue("MASK_LEFT_PERCENT").Trim(), out left);
            float.TryParse(ConfigValues.GetConfigValue("MASK_WIDTH_PERCENT").Trim(), out width);
            float.TryParse(ConfigValues.GetConfigValue("MASK_HEIGHT_PERCENT").Trim(), out height);
            float.TryParse(ConfigValues.GetConfigValue("MASK_OPACITY").Trim(), out opacity);

            ImageUtilities.DrawLayerMask(serverFileName, Server.MapPath("../" + Constant.MASK_IMAGE_FILE), top, left, width, height, ResizeOptions.MaximumSizeFix, opacity, ImageUtilities.HIGHT_PROGRESSIVE);
        }
        // end
    }

    public void SetCookie(string name, string value)
    {
        //CookieUtility.SetCookie(name, value, DateTime.Now.AddDays(1), Request, Response);
        Session[name] = value;
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }
}
